<?php
/**
 * 个人空间
 */
class SpaceAction extends Action{

	function _initialize() {
		if (!is_numeric($_GET['uid']) && is_string($_GET['uid'])) {
			$domainuser = D('User')->getUserByIdentifier(h($_GET['uid']), 'domain');
	   		if ($domainuser) {
	   			$this->uid = $domainuser['uid'];
	   			$this->assign('uid',$this->uid);
	   		}else {
   	   			$this->error('用户不存在');
	   		}
		}

		if ('detail' != ACTION_NAME) {
			$user_info = D('User')->getUserByIdentifier($this->uid);
	   	   	if ($user_info)
				$this->assign('userinfo',$user_info);
	
	       	$this->__getSpaceCount( $this->uid );
		}
	}

	private function __getSpaceCount($uid) {
		$followInfo = getUserFollow($uid);
		$data['followstate'] = D('Follow','weibo')->getState($this->mid, $uid, 0);
		$data['isBlackList'] = isBlackList($this->mid,$uid);
		$data['privacy']     = D('UserPrivacy','home')->getPrivacy($this->mid,$uid);
		$data['spaceCount']['miniblog']   = model('UserCount')->getUserWeiboCount($uid);
		$data['spaceCount']['following']  = $followInfo['following'];
		$data['spaceCount']['follower']   = $followInfo['follower'];
		$data['spaceCount']['message']   = 0;
		$data['hotTopic'] =  D('Topic','weibo')->getHot();
		$data['usertags'] =  D('UserTag')->getUserTagList( $this->uid );
        $data['verified'] = M('user_verified')->field('verified,info')->where("uid={$this->uid}")->find();
		$this->assign( $data );
	}

    // 用户空间首页
    function index() {
    	$strType = h($_GET['type']);
        $data['list'] = D('Operate','weibo')->getSpaceList($this->uid, $strType);
        $data['user'] = D('User')->getUserByIdentifier($this->uid);
        $data['type'] = $strType;

		// 被浏览积分
		if($this->uid != $this->mid)
			X('Credit')->setUserCredit($this->uid,'space_visited');

        $this->assign( $data );
        $this->setTitle($data['user']['uname'] . '的空间');
    	$this->display();
    }

    // 查看微博详细
    function detail(){
    	$intId = intval( $_GET['id'] );
	    $data['mini']      =  D('Operate','weibo')->getOneLocation( $intId );
	    if(!$data['mini']) $this->error('提交错误参数');
		$data['comment']   =  D('Comment','weibo')->getComment( $intId );
		$data['privacy'] = D('UserPrivacy','home')->getPrivacy($this->mid,$data['mini']['uid']);
    	$this->assign( $data );
	    $this->uid = $data['mini']['uid'];

		$user_info = D('User')->getUserByIdentifier($this->uid);
	   	if ($user_info) {
			$this->assign('userinfo',$user_info);
	   	}
	    $this->__getSpaceCount( $this->uid );

	    $this->setTitle(getUserName($this->uid) . '的微博');
    	$this->display();
    }

    //关注
    function follow(){
    	$data['type'] = ($_GET['type']=='follower')?'follower':'following';
    	if($data['type'] == 'following'){
    		//关注分组列表
	    	$data['gid']  = is_numeric($_GET['gid'])?$_GET['gid']:'all';
	    	$group_list = D('FollowGroup','weibo')->getGroupList($this->uid);
	    	//调整分组列表
	    	if(!empty($group_list)){
		    	$group_count = count($group_list);
		    	for($i=0;$i<$group_count;$i++){
		    		if($group_list[$i]['follow_group_id'] != $data['gid']){
		    			$group_list[$i]['title'] = (strlen($group_list[$i]['title'])+mb_strlen($group_list[$i]['title'],'UTF8'))/2>8?getShort($group_list[$i]['title'],3).'...':$group_list[$i]['title'];
		    		}
		    		if($i<2){
		    			$data['group_list_1'][] = $group_list[$i];
		    		}else{
		    			if($group_list[$i]['follow_group_id'] == $data['gid']){
		    				$data['group_list_1'][2]  = $group_list[$i];
		    				continue;
		    			}
		    			$data['group_list_2'][] = $group_list[$i];
		    		}
		    	}
		    	if(empty($data['group_list_1'][2]) && !empty($data['group_list_2'][0])){
		    		$data['group_list_1'][2] = $data['group_list_2'][0];
		    		unset($data['group_list_2'][0]);
		    	}
	    	}
    	}
    	// 关注的人列表
    	$data['list'] = D('Follow','weibo')->getList($this->uid,$data['type'],0,$data['gid']);

    	$this->assign($data);
    	$this->setTitle(getUserName($this->uid) . '的' . ($data['type'] == 'follower' ? '粉丝' : '关注'));
    	$this->display();
    }



    //个人资料
    function profile(){
    	$pUserProfile = D('UserProfile');
    	$pUserProfile->uid = $this->uid;
    	$data['userInfo']  = $pUserProfile->getUserInfo();
    	$this->assign( $data );
    	$this->setTitle(getUserName($this->uid) . '的详细资料');
    	$this->display();
    }
}

?>